
*****
* Figure 10 b
*****
set printcolor gs1, permanently

** Baseline f=17.6

use INTERMEDIATE/data_pooled.dta, clear

* DETERMINE AGE
*Defines cohort/entryTime, exitTime, age, and MaxAge
bysort product: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort product: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort product: egen maxAge=max(age) 
bysort product: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

gen Nentry=(type=="entry"| type=="entry/exit")
gen Nexit=(type=="exit"|type=="entry/exit")

gen N_it=1 
gen size_it = prod_quality^5

* Becomes a firmx year dataset
collapse (sum) N_it Nentry Nexit size_it (mean) log_q log_Q , by(firm seed fe quarter)

* Firm age variables
bysort firm: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort firm: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort firm: egen maxAge=max(age) 
bysort firm: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

* Variables
gen log_size_it=log(size_it)
gen log_size_itA=log(size_it)-5*log_q+(2-6)*log_Q
gen comp2=log(N_it)
gen comp1=log_size_it -  comp2
gen comp1A=log_size_itA -  comp2

gen sample=0
replace sample=1 if (cohort>=2 & cohort<=23) & age<=16 & age>0 & maxAge>16 & flag_longitudinal=="Complete"
replace age=17 if sample==0 | age>16

qui tab cohort, gen(cohort_d)
foreach num of numlist 4/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d3-(`num'-2)*cohort_d2)
}


areg  log_size_it i.age cohort_nd* if sample==1, absorb(fe)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)
save INTERMEDIATE/LifeCycle_firm.dta, replace

** Counterfactual f=0

use INTERMEDIATE/data_pooled_f0.dta, clear

* DETERMINE AGE
*Defines cohort/entryTime, exitTime, age, and MaxAge
bysort product: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort product: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort product: egen maxAge=max(age) 
bysort product: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

gen Nentry=(type=="entry"| type=="entry/exit")
gen Nexit=(type=="exit"|type=="entry/exit")

gen N_it=1 
gen size_it = prod_quality^5

* Becomes a firmx year dataset
collapse (sum) N_it Nentry Nexit size_it (mean) log_q log_Q , by(firm seed fe quarter)

* Firm age variables
bysort firm: egen x=min(quarter)
gen lcensored=(x==1)
gen cohort=x 
bysort firm: egen y=max(quarter)
qui su y
gen rcensored=(y==`r(max)') 
gen exitTime=y 
gen age=quarter-cohort 
bysort firm: egen maxAge=max(age) 
bysort firm: egen totalobs=count(quarter)

* Type of observation 
gen type="NA"
replace type="entry"      if age==0 & age!=maxAge  
replace type="continues"  if age>0  & age<maxAge  
replace type="exit"       if age==maxAge & age!=0 
replace type="entry/exit" if age==maxAge & age==0 
gen censored="NA"  // NEW & CORRECTED
replace censored="left censored"   if lcensored==1   & rcensored==0
replace censored="right censored"  if rcensored==1 & lcensored==0
replace censored="left censored & right censored" if lcensored==1  & rcensored==1   
replace censored="not censored" if lcensored==0  & rcensored==0
tab type censored, m

gen flag_longitudinal="NA"
replace flag_longitudinal="Complete" if totalobs==y-x+1
replace flag_longitudinal="Incomplete" if totalobs<y-x+1

* Variables
gen log_size_it=log(size_it)
gen log_size_itA=log(size_it)-5*log_q+(2-6)*log_Q
gen comp2=log(N_it)
gen comp1=log_size_it -  comp2
gen comp1A=log_size_itA -  comp2

gen sample=0
replace sample=1 if (cohort>=2 & cohort<=23) & age<=16 & age>0 & maxAge>16 & flag_longitudinal=="Complete"
replace age=17 if sample==0 | age>16

qui tab cohort, gen(cohort_d)
foreach num of numlist 4/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d3-(`num'-2)*cohort_d2)
}

areg  log_size_it i.age cohort_nd* if sample==1, absorb(fe)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)
save INTERMEDIATE/LifeCycle_firm_f0.dta, replace





use INTERMEDIATE/LifeCycle_firm.dta, clear
gen baseline=1
append using INTERMEDIATE/LifeCycle_firm_f0.dta
replace baseline=0 if baseline==.

twoway ///
(line coef age if age<=16 & baseline==1, color(midblue) lwidth(vthick)) ///
(line coef age if age<=16 & baseline==0, color(maroon) lwidth(vthick) lpattern(dash)) ///
, ytitle("Estimated Sales (log scale)") xtitle("Age (quarters)") xlabel(4(4)16) ///
title("") ///
legend(off) ysize(4.7) ///
text(.57 10.1 "Baseline f=17.6")  text(.26 14 "Counterfactual f=0") ///
graphregion(color(white)) plotregion(fcolor(white)) legend(region(lcolor(white))) note("")
graph export OUTPUT/Figure10b.eps, replace
